package com.resifind.www.dao.impl;

import com.resifind.www.dao.RoomDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.Room;
import com.resifind.www.po.RoomNumber;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class RoomDaoImpl implements RoomDao {

    @Override
    public Room queryRoomByRoomId(int roomId) {
        //language=MySQL
        String sql = "select * from resi_find.room where id = ?";
        Object[] params = {roomId};
        List<Room> rooms = Mappers.roomMapper(sql, params);
        return rooms.isEmpty() ? null : rooms.getFirst();
    }

    @Override
    public List<Room> queryRoomByMerchantId(int merchantId) {
        //language=MySQL
        String sql = "select * from resi_find.room where merchant_id = ?";
        Object[] params = {merchantId};
        return Mappers.roomMapper(sql, params);
    }

    @Override
    public boolean deleteRoom(int roomId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.room where id = ?";
        Object[] params = {roomId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean addRoom(int merchantId, Room room, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.room (merchant_id, keyword, bed_type, facility, area, capacity, price) values (?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {merchantId, room.getKeyword(), room.getBedType(), room.getFacility(), room.getArea(), room.getCapacity(), room.getPrice()};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateRoom(Room room, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.room set keyword = ?, bed_type = ?, facility = ?, area = ?, capacity = ?, price = ? where id = ?";
        Object[] params = {room.getKeyword(), room.getBedType(), room.getFacility(), room.getArea(), room.getCapacity(), room.getPrice(), room.getId()};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateRoomSalesAndSurplus(int roomId, int num, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.room set sales_volume = sales_volume + ?, surplus = surplus - ? where id = ?";
        Object[] params = {num, num, roomId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public List<RoomNumber> queryRoomNumberByRoomId(int roomId) {
        //language=MySQL
        String sql = "select * from resi_find.room_number where room_id = ?";
        Object[] params = {roomId};
        return Mappers.roomNumberMapper(sql, params);
    }

    @Override
    public int queryAvailableRoomNumberByRoomId(int roomId) {
        //language=MySQL
        String sql = "select count(*) from resi_find.room_number where room_id = ? and status = '可用'";
        Object[] params = {roomId};
        List<Integer> query = JdbcUtils.query(sql, params, rs -> rs.getInt(1));
        return query.getFirst();
    }

    @Override
    public boolean updateRoomSurplusByRoomIdAndNum(int roomId, int num, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.room set surplus = ? where id = ?";
        Object[] params = {num, roomId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean addRoomNumbers(int roomId, String numbers, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.room_number (room_id, room_number) values (?, ?)";
        Object[] params = {roomId, numbers};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public RoomNumber queryRoomNumberByRoomNumberId(int roomNumberId) {
        //language=MySQL
        String sql = "select * from resi_find.room_number where id = ?";
        Object[] params = {roomNumberId};
        List<RoomNumber> roomNumbers = Mappers.roomNumberMapper(sql, params);
        return roomNumbers.isEmpty() ? null : roomNumbers.getFirst();
    }

    @Override
    public boolean deleteRoomNumber(int roomNumberId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.room_number where id = ?";
        Object[] params = {roomNumberId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean deleteRoomNumberByRoomId(int roomId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.room_number where room_id = ?";
        Object[] params = {roomId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateRoomNumberStatus(int roomNumberId, String status, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.room_number set status = ? where id = ?";
        Object[] params = {status, roomNumberId};
        return JdbcUtils.update(sql, params, conn);
    }

}
